package com.fc.push.handler.pub.exec.task;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.fc.commons.JsonUtils;
import com.fc.push.exception.MServerException;
import com.fc.push.logger.Applogger;
import com.fc.push.logger.Errorlogger;
import com.fc.remoting.core.SessionManager;

/**
 * 网页推送任务
 * 
 * @author Administrator
 *
 */
public class WebPublishTask implements Runnable, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JSONObject msg = null;
	private List<String> idList = null;
	private Object[] ids = null;

	private MODE mm = MODE.LIST;

	public WebPublishTask(List<String> ids, JSONObject msg) {
		this.msg = msg;
		this.idList = ids;
	}

	public WebPublishTask(Object[] ids, JSONObject msg) {
		this.msg = msg;
		this.ids = ids;

		mm = MODE.ARRAY;
	}

	public static enum MODE {
		LIST, ARRAY
	}

	@Override
	public void run() {
		switch (mm) {
		case LIST:
			Applogger.instance().getLogger().info("run()");
			runList();
			break;
		case ARRAY:
			Applogger.instance().getLogger().info("run()");
			runArray();
			break;
		default:
			break;
		}
	}

	// private void runList(){
	// FullHttpResponse response = null;
	// try {
	// response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
	// HttpResponseStatus.OK,
	// Unpooled.wrappedBuffer(JsonUtils.object2String(msg).getBytes("UTF-8")));
	// } catch (UnsupportedEncodingException e) {
	// e.printStackTrace();
	// }
	// response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN,
	// "*");
	// response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
	// response.headers().set(HttpHeaders.Names.CONTENT_LENGTH,
	// response.content().readableBytes());
	// for(String id:idList){
	// Channel channel = SessionManagerHandler.getHttpSession(id);
	// if(channel == null) continue;
	// Applogger.instance().getLogger().info(JsonUtils.object2String(response));
	// if(channel.isActive()){
	// Applogger.instance().getLogger().info(JsonUtils.object2String(response));
	// channel.writeAndFlush(response);
	// }
	// }
	// }

	private void runList() {
		for (String id : idList) {
			HttpServletResponse response = SessionManager.getHttpChannel(id);
			if (response == null)
				continue;
			Applogger.instance().getLogger().info(JsonUtils.object2String(response));
			try {
				response.getWriter().write(msg.toJSONString());
			} catch (IOException e) {
				Errorlogger.instance().getLogger().error(e.getMessage(), e);
				throw new MServerException(e, e.getMessage());
			}
		}
	}

	private void runArray() {

		for (Object id : ids) {
			HttpServletResponse response = SessionManager.getHttpChannel(id.toString());
			if (response == null)
				continue;
			Applogger.instance().getLogger().info(JsonUtils.object2String(response));
			try {
				response.getWriter().write(msg.toJSONString());
			} catch (IOException e) {
				Errorlogger.instance().getLogger().error(e.getMessage(), e);
				throw new MServerException(e, e.getMessage());
			}
		}
	}
}
